#include "Scuba.h"
#include "UBoat.h"

#include "ArgumentParser.h"
#include "Helper.h"

#include <string>
#include <iostream>
#include <stdio.h>

using namespace std;

CArgumentParser* gpxArgumentParser;
string gsTarget = "./test";
string gsScriptSourceDir = "./uboat";

UBoat gxUBoat;

int parseCommandLineUBoat(int argc, char**argv)
{
	gpxArgumentParser=new CArgumentParser("i:x:a:h");

	gpxArgumentParser->m_Helper->fillProgramName("nUBoat");
	gpxArgumentParser->m_Helper->fillAuthor("sonofdelphi");
	gpxArgumentParser->m_Helper->fillSummary("Create Browse-Packs for your codebase");
	gpxArgumentParser->m_Helper->fillUsageLine("[OPTIONS] <Directory>");
	gpxArgumentParser->fillHelpForOption('x',"EXclude/ignore directory/file");
	gpxArgumentParser->fillHelpForOption('a',"UBoat Application home directory - location of scripts");
	gpxArgumentParser->fillHelpForOption('h',"Displays this help message");

	gpxArgumentParser->m_Helper->fillYear("2009");
	gpxArgumentParser->m_Helper->fillOwner("The Oraclique");
	gpxArgumentParser->m_Helper->fillTailMessage("Part of Neptuner Code-Management Suite.\nBuild:" __DATE__"/"__TIME__  "\nThis program comes with absolutely no warranty.");

	int nParseEndPos = gpxArgumentParser->parse(argc,argv);
	if(!gpxArgumentParser->wasParseSuccessful())
	{
		gpxArgumentParser->m_Helper->displayHelp();
		delete gpxArgumentParser;
		cerr<<"Invalid option supplied"<<endl;
		return -1;
	}
	
	gxUBoat.m_sScriptSourceDir = gpxArgumentParser->wasOptionSupplied('a')? gpxArgumentParser->getField('a'):gsScriptSourceDir;

	int i,n;
	string sValue;

	if(gpxArgumentParser->wasOptionSupplied('i')){
		n= gpxArgumentParser->getFieldCardinal('i');
		for(i=0;i<n;i++){
			sValue=gpxArgumentParser->getField('i',i);
			gxUBoat.m_asInclude.push_back(sValue);
		}
	}

	if(gpxArgumentParser->wasOptionSupplied('x')){
		n= gpxArgumentParser->getFieldCardinal('x');
		for(i=0;i<n;i++){
			sValue=gpxArgumentParser->getField('x',i);
			gxUBoat.m_asExclude.push_back(sValue);
		}
	}

 	return nParseEndPos+1;
}

int main(int argc, char**argv)
{
	int nProgArgIndex = parseCommandLineUBoat(argc, argv);
	if(nProgArgIndex<0)
		return 1;

	if(gpxArgumentParser->wasOptionSupplied('h'))
	{
		gpxArgumentParser->m_Helper->displayHelp();
		delete gpxArgumentParser;
		return 0;
	}

	if(nProgArgIndex<argc)
	{
			gsTarget=argv[nProgArgIndex];
	}
	else
	{
			cerr<<"Attempting to proceed with default directory: "<<gsTarget<<endl;
	}


	gxUBoat.navigate(gsTarget,"","");

	if(argc==1)
	{
		cerr<<"Press ENTER to continue..";
		getchar();
	}

    return 0;
}


